package com.neulogistics.contrib.procuredcenter.api.application.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.procuredcenter.api.application.query.PurchaseOrderQuery;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.PurchaseOrderPO;
import org.springframework.beans.BeanUtils;

public class PurchaseOrderQueryPOConverter {
    private static final int Rate = 100;
    public static PurchaseOrderPO queryToPO(PurchaseOrderQuery purchaseOrderQuery){
        if(purchaseOrderQuery == null){
            return null;
        }

        PurchaseOrderPO purchaseOrderPO = new PurchaseOrderPO();

        BeanUtils.copyProperties(purchaseOrderQuery, purchaseOrderPO);

        if(purchaseOrderQuery.getPurchaseType() != null){
            purchaseOrderPO.setPurchaseType(purchaseOrderQuery.getPurchaseType().id);
        }

        if(purchaseOrderQuery.getPurchaseItem() != null){
            purchaseOrderPO.setPurchaseItem(JSONUtil.parseObj(purchaseOrderQuery.getPurchaseItem()));
        }

        if(purchaseOrderQuery.getPurchaseTotal() != null){
            purchaseOrderPO.setPurchaseTotal((int) (purchaseOrderQuery.getPurchaseTotal() * Rate));
        }

        return purchaseOrderPO;
    }
}
